ifeq ("$(BUILD_STYLE)","Debug")
   DEBUG := -debug
   CONFIG := Debug
else
   DEBUG :=
   CONFIG := Release
endif

HAXE_OS := $(PLATFORM_NAME)
ifeq ("$(HAXE_OS)","iphonesimulator")
   HAXE_OS := iphonesim
endif
CONFIG := $(CONFIG)-$(HAXE_OS)


LIB_FILE := build/$(CONFIG)/ApplicationMain$(DEBUG).$(HAXE_OS).a

ifeq ("$(ACTION)","clean")
default: clean
else
default: build-haxe
endif


export HXCPP_COMPILE_THREADS := 2

build-haxe:
	@echo "Haxe  CONFIG : $(CONFIG)"
	::foreach preBuildScripts::
		::script::::foreach args:: "__current__"::end::
	::end::
	mkdir -p build/$(CONFIG)
	haxe Build.hxml -D iphone -D $(HAXE_OS) -cpp build/$(CONFIG) $(DEBUG)
	::foreach postBuildScripts::
		::script::::foreach args:: "__current__"::end::
	::end::
	haxelib run hxcpp build/$(CONFIG)/obj/Build.xml $(DEBUG)::foreach COMPILER_FLAG:: -D__current__::end::
	::foreach preInstallScripts::
		::script::::foreach args:: "__current__"::end::
	::end::
	if ( !(cmp -s $(LIB_FILE) ../lib/ApplicationMain.a) ); then echo "Copy library..."; cp $(LIB_FILE) ../lib/ApplicationMain.a; fi
	::foreach postInstallScripts::
		::script::::foreach args:: "__current__"::end::
	::end::
clean:
	rm -rf build
